package com.xiangshizhineng.tianzhangzhi.app.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;


/**
 * http通讯类
 *
 */
@Slf4j
public class HttpClientUtil {

    /**
     * 密钥
     */
    private final static String appKey = "3.1415926abcdefg";

    public static String sendPost(String url, String json) throws Exception {
        return getString(url, json);
    }

    private static String getString(String url, String json) throws IOException {
        log.info("请求报文：" + json);
        // 这是默认返回值，接口调用失败
        String returnValue;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            // 第一步：创建HttpClient对象
            httpClient = HttpClients.createDefault();

            // 第二步：创建httpPost对象
            HttpPost httpPost = new HttpPost(url);

            //第三步：给httpPost设置JSON格式的参数
            StringEntity requestEntity = new StringEntity(json, "utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setHeader("Content-type", "application/json");
            Long timestamp = System.currentTimeMillis();
            httpPost.setHeader("sign", MD5FY.MD5Encode(appKey + timestamp));
            httpPost.setHeader("timestamp", timestamp.toString());
            httpPost.setEntity(requestEntity);

            //第四步：发送HttpPost请求，获取返回值
            returnValue = httpClient.execute(httpPost, responseHandler);
            log.info("返回报文：" + returnValue);
        } catch (Exception e) {
            log.error("post请求异常,url: {} \n param: {} \n error: {}", url, json, e);
            throw e;
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                log.error("httpclient链接关闭异常: {}", e);
            }
        }
        // 第五步：处理返回值
        return returnValue;
    }

    public static String sendGet(String url) {
        log.info("请求报文：" + url);
        // 这是默认返回值，接口调用失败
        String returnValue = "100400";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            // 第一步：创建HttpClient对象
            httpClient = HttpClients.createDefault();

            // 第二步：创建HttpGet对象
            HttpGet httpGet = new HttpGet(url);

            //v第三步：发送HttpGet请求，获取返回值
            returnValue = httpClient.execute(httpGet, responseHandler);
            log.info("返回报文：" + returnValue);
        } catch (Exception e) {
            log.error("get请求异常,url: {} \n param: {} \n error: {}", url, e);
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                log.error("httpclient链接关闭异常: {}", e);
            }
        }
        // 第五步：处理返回值
        return returnValue;
    }

    public static String sendPut(String url, String json) throws Exception {
        log.info("请求报文：" + json);
        // 这是默认返回值，接口调用失败
        String returnValue;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            // 第一步：创建HttpClient对象
            httpClient = HttpClients.createDefault();

            // 第二步：创建HttpPut对象
            HttpPut httpPut = new HttpPut(url);

            //第三步：给HttpPut设置JSON格式的参数
            StringEntity requestEntity = new StringEntity(json, "utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPut.setHeader("Content-type", "application/json");
            httpPut.setEntity(requestEntity);

            //第四步：发送HttpPut请求，获取返回值
            returnValue = httpClient.execute(httpPut, responseHandler);
            log.info("返回报文：" + returnValue);
        } catch (Exception e) {
            log.error("put请求异常,url: {} \n param: {} \n error: {}", url, json, e);
            throw e;
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                log.error("httpclient链接关闭异常: {}", e);
            }
        }
        // 第五步：处理返回值
        return returnValue;
    }
}
